var $;
layui.use(['layer', 'form', 'jquery', 'table'], function () {
    var layer = layui.layer;
    var form = layui.form;
    $ = layui.jquery;
    var table = layui.table;
    //加载表格
    getTable();

    function getTable() {
        $.ajax({
            url: '/closure/getClosure',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    var tableIns;

    function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            toolbar: true,
            data: data,
            async: true,
            title: '零消费用户列表',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            cols: [[
                {type: 'checkbox', with: 50},
                {
                    field: '', title: '   序号', align: 'center', templet: function (data) {
                        return data.LAY_INDEX;
                    }
                },
                {field: 'userCode', title: '用户编号', align: 'center'},
                {field: 'startDate', title: '封停时间', align: 'center'},
                {field: 'endDate', title: '解封时间', align: 'center'},
                {field: 'reason', title: '封停原因', align: 'center'},
                {
                    field: 'days', title: '封停天数', align: 'center', templet: function (data) {
                        if (data.days == 0) {
                            return "已解封";
                        } else {
                            return data.days + "天";
                        }
                    }
                },
                {
                    field: 'type', title: '用户', align: 'center', templet: function (data) {
                        if (data.type == 2) {
                            return "前端用户";
                        }
                    }
                },
                {
                    fiele: '', title: '操作', align: 'center', templet: function (data) {
                        return "<div><a href='javascript:unseal(\"" + data.userId + "\",\"" + data.days + "\",\"" + data.reason + "\",\"" + data.id + "\");'" +
                            " style='margin-right: 10px;color: #009688'>解封</a><a href='javascript:deleteClosure(\"" + data.id + "\");' " +
                            "style='margin-right: 10px;color: #009688'>删除</a></div>";
                    }
                }
            ]]
        });
    }

    //解封
    unseal = function unseal(userId, days, reason, id) {
        if (days == 0) {
            layer.msg("该账户已被解封");
        } else {
            $.ajax({
                url: '/user/getinformation',
                type: "post",
                dataType: 'json',
                data: {'userId': userId},
                success: function (data) {
                    layer.open({
                        type: 2,
                        title: '解封用户',
                        offset: 'auto',
                        shade: 0,
                        maxmin: true,
                        area: ['450px', '410px'],
                        content: '/user/user-unseal.html',
                        btn: ['确定', '取消'],
                        success: function (layero, index) {
                            var obj = layero.find('iframe').contents();
                            obj.find('input[name=account]').val(data.data.account);
                            obj.find('input[name=nickname]').val(data.data.nickname);
                            obj.find('input[name=reason]').val(reason);
                        },
                        yes: function (index, layero) {
                            var obj = layero.find('iframe').contents();
                            var remarks = obj.find('#remarks').val();
                            //修改状态
                            $.ajax({
                                url: '/user/updateStatus',
                                type: 'post',
                                dataType: 'json',
                                data: {
                                    'userId': userId,
                                    'status': 2
                                },
                                success: function (data) {
                                    //添加解封日志
                                    var unseal = {
                                        'userId': userId,
                                        'type': 1,
                                        'remarks': remarks
                                    }
                                    $.ajax({
                                        url: '/unseal/addUnseal',
                                        type: 'post',
                                        dataType: 'json',
                                        data: unseal,
                                        success: function (data) {
                                            //修改天数
                                            $.ajax({
                                                url: '/closure/updateClosure',
                                                type: 'post',
                                                dataType: 'json',
                                                data: {'id': id, 'days': 0},
                                                success: function (data) {
                                                    layer.msg("解封成功");
                                                    getTable();
                                                    layer.close(index);
                                                }
                                            });
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }
    }
    //删除
    deleteClosure = function deleteClosure(id) {
        layer.confirm('是否确定删除此条数据', {
                icon: 3, title: '提示'
            },
            function (index) {
                //通过id删除表格数据
                $.ajax({
                    url: '/closure/deleteClosure',
                    type: 'post',
                    dataType: 'json',
                    data: {'id': id},
                    success: function (data) {
                        getTable();
                        layer.msg("删除成功");
                    }
                });

            }
        )
    }
    //搜索
    form.on('submit(search)', function (data) {
        $.ajax({
            url: '/closure/getClosure',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                tablereload(data.data);
            }
        });
    });
});

//返回
function goBack() {
    //通过页面查找iframe
    parent.$('#recordLog').val($('#showDown').text());
    parent.before();
    return false;
}